package com.zkzx.driver.service;

import com.zkzx.common.driver.command.DriverCommand;
import com.zkzx.common.driver.model.DriverLicenseVO;
import com.zkzx.common.driver.vo.DriverAlidityVO;
import com.zkzx.driver.mapper.DriverLicenseMapper;
import com.zkzx.driver.mapper.entity.DriverLicense;
import com.zkzx.driver.mapper.entity.DriverLicenseExample;
import com.zkzx.common.driver.constants.DriverAlidityEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/08/10:24
 * @Description:
 */
@Service
public class DriverLicenseService {

    @Resource
    private DriverLicenseMapper driverLicenseMapper;

    /**
     * 根据司机id查询驾驶证信息
     * @param driverId 企业id
     * @return 驾驶证信息
     */
    public DriverLicenseVO findVOByDriverId(Integer driverId){
        DriverLicense driverLicense = findByDriverId(driverId);
        if(Objects.isNull(driverLicense)){
            return null;
        }
        return DriverLicenseVO.builder()
                .authority(driverLicense.getAuthority())
                .type(driverLicense.getType())
                .driverAlidityVO(DriverAlidityVO.builder()
                        .value(DriverAlidityEnum.valueOf(driverLicense.getStatus()).name())
                        .description(DriverAlidityEnum.valueOf(driverLicense.getStatus()).getDescription())
                        .build())
                .startTime(driverLicense.getStarttime())
                .endTime(driverLicense.getEndtime())
                .frontPic(driverLicense.getFrontpic())
                .subpagePic(driverLicense.getSubpagepic())
                .build();
    }

    /**
     * 根据司机id查询驾驶证信息
     * @param driverId 企业id
     * @return 驾驶证信息
     */
    public DriverLicense findByDriverId(Integer driverId){
        DriverLicenseExample driverLicenseExample = new DriverLicenseExample();
        driverLicenseExample.createCriteria().andDriveridEqualTo(driverId);
        Optional<DriverLicense> driverLicenseOne = driverLicenseMapper.selectByExample(driverLicenseExample).stream().findFirst();

        DriverLicense driverLicense  = null;
        if(driverLicenseOne.isPresent()){
            driverLicense = driverLicenseOne.get();
        }
        return driverLicense;
    }

    /**
     * 更新驾驶证信息
     * @param command 入参
     * @param id 司机id
     */
    public void update(DriverCommand command, Integer id){
        if(Objects.isNull(command.getDriverLicense())){
            return;
        }
        DriverLicense driverLicense = new DriverLicense();
        driverLicense.setId(command.getDriverLicense().getId());
        driverLicense.setAuthority(command.getDriverLicense().getAuthority());
        driverLicense.setType(command.getDriverLicense().getType());
        driverLicense.setStatus(command.getDriverLicense().getDriverAlidityVO().getValue());
        driverLicense.setStarttime(command.getDriverLicense().getStartTime());
        driverLicense.setEndtime(command.getDriverLicense().getEndTime());
        driverLicense.setFrontpic(command.getDriverLicense().getFrontPic());
        driverLicense.setSubpagepic(command.getDriverLicense().getSubpagePic());
        DriverLicenseExample driverLicenseExample = new DriverLicenseExample();
        DriverLicenseExample.Criteria criteriaLicense = driverLicenseExample.createCriteria();
        criteriaLicense.andDriveridEqualTo(id);
        driverLicenseMapper.updateByExampleSelective(driverLicense,driverLicenseExample);

    }

    /**
     * 插入驾驶证信息
     * @param command 入参
     * @param id 司机id
     */
    public void insert(DriverCommand command, Integer id){

        if(Objects.isNull(command.getDriverLicense())){
            return;
        }

        DriverLicense driverLicense = new DriverLicense();
        driverLicense.setDriverid(id);
        Optional.ofNullable(command.getDriverLicense()).ifPresent(dl->{
            driverLicense.setAuthority(command.getDriverLicense().getAuthority());
            driverLicense.setType(command.getDriverLicense().getType());
            driverLicense.setStatus(command.getDriverLicense().getDriverAlidityVO().getValue());
            driverLicense.setStarttime(command.getDriverLicense().getStartTime());
            driverLicense.setEndtime(command.getDriverLicense().getEndTime());
            driverLicense.setFrontpic(command.getDriverLicense().getFrontPic());
            driverLicense.setSubpagepic(command.getDriverLicense().getSubpagePic());
        });

        driverLicenseMapper.insert(driverLicense);

    }

    public void insertOrUpdateLicense(DriverCommand command, Integer id){
        if (Objects.nonNull(command.getDriverLicense())){
            DriverLicense driverLicense = new DriverLicense();
            driverLicense.setDriverid(id);
            driverLicense.setAuthority(command.getDriverLicense().getAuthority());
            driverLicense.setType(command.getDriverLicense().getType());
            driverLicense.setStatus(command.getDriverLicense().getDriverAlidityVO().getValue());
            driverLicense.setStarttime(command.getDriverLicense().getStartTime());
            driverLicense.setEndtime(command.getDriverLicense().getEndTime());
            driverLicense.setFrontpic(command.getDriverLicense().getFrontPic());
            driverLicense.setSubpagepic(command.getDriverLicense().getSubpagePic());

            DriverLicense byDriverId = this.findByDriverId(id);

            if(Objects.isNull(byDriverId)){
                driverLicenseMapper.insert(driverLicense);
                return;
            }

            DriverLicenseExample driverLicenseExample = new DriverLicenseExample();
            driverLicenseExample.createCriteria().andIdEqualTo(byDriverId.getId());

            driverLicenseMapper.updateByExampleSelective(driverLicense,driverLicenseExample);

        }

    }
}
